package org.jeecg.service.service;

import org.jeecg.domain.Fans;
import org.jeecg.domain.User;
import org.jeecg.mapper.FansMapper;
import org.jeecg.service.FansService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class FansServiceImpl implements FansService {

    @Autowired
    private FansMapper fansMapper;

    @Override
    public List<User> getFans(String username) {
        List<User> fans;
        try {
            fans = fansMapper.getFans(username);
        }catch (Exception e){
            return new ArrayList<>();
        }
        return fans;
    }

    @Override
    public Boolean addFan(String username, String fanUsername) {
        UUID uuid = UUID.randomUUID();
        //生成一个16位的UUID
        String id = uuid.toString().replace("-","").substring(0,16);
        Date date = new Date();
        Fans fans = new Fans(id,username,fanUsername,date);
        Integer result = fansMapper.insertFans(fans);
        if (result > 0){
            return true;
        }
        return false;
    }

    @Override
    public Boolean queryFans(String username, String careUsername) {
        Fans fans = fansMapper.queryFan(username, careUsername);
        if (fans != null){
            return true;
        }
        return false;
    }

    @Override
    public Boolean cancelCare(String username, String fansUsername) {
        Integer result = fansMapper.deleteFans(username, fansUsername);
        if (result > 0){
            return true;
        }
        return false;
    }
}
